package com.ciquan.server.service.impl;

import com.ciquan.server.bean.PhoneCode;
import com.ciquan.server.bean.UnReply;
import com.ciquan.server.dao.PhoneCodeMapper;
import com.ciquan.server.service.PhoneCodeService;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.util.DateUtils;
import com.ciquan.server.util.web.Result;
import com.cloopen.rest.sdk.CCPRestSDK;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by card on 2015/8/29.
 */
@Service
public class PhoneCodeServiceImpl implements PhoneCodeService {

    @Resource
    private PhoneCodeMapper phoneCodeMapper;

    @Resource
    private UserInfoService userInfoService;

    private CCPRestSDK restAPI;

    public PhoneCodeServiceImpl() {
        restAPI = new CCPRestSDK();
        restAPI.init("sandboxapp.cloopen.com", "8883");
        restAPI.setAccount("aaf98f894e7826aa014e87d550cf0d59", "fa4a34b965d84c478864aea56630d907");
        restAPI.setAppId("8a48b5514e7c2193014e87d8be710ac9");
    }

    @Override
    public boolean send(PhoneCode phoneCode, Result result) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("phone", phoneCode.getPhone());
        map.put("createTime", DateUtils.today());
        int count = phoneCodeMapper.selectCount(map);
        if(count > 3) {
            result.setSuccess(false);
            result.setStatusmsg("同一个号码每天只能发送三次验证码");
            return false;
        }

        Map<String, Object> smsResult = restAPI.sendTemplateSMS(phoneCode.getPhone(), "26163", new String[]{phoneCode.getCode()});
        if("000000".equals(smsResult.get("statusCode")) && phoneCodeMapper.insert(phoneCode) > 0){
            result.setSuccess(true);
            result.setStatusmsg("发送验证码成功");
            return true;
        }
        else{
            result.setStatusmsg("发送验证码失败");
            return false;
        }
    }

    @Override
    public boolean validate(String phone, String code) {
        PhoneCode result = phoneCodeMapper.selectLast(phone);
        if(result != null && StringUtils.equals(code, result.getCode())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean applyUser(String phone) {
        Map<String, Object> smsResult = restAPI.sendTemplateSMS(phone, "40435", new String[]{""});
        if("000000".equals(smsResult.get("statusCode"))){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean applyRefuse(String phone) {
        Map<String, Object> smsResult = restAPI.sendTemplateSMS(phone, "40434", new String[]{""});
        if("000000".equals(smsResult.get("statusCode"))){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean unReply(UnReply unReply) {
        Map<String, Object> smsResult = restAPI.sendTemplateSMS(unReply.getMobile(), "41551", new String[]{unReply.getTime(), unReply.getArtistName(), unReply.getWorksName()});
        if("000000".equals(smsResult.get("statusCode"))){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public void sendAll() {
        List<String> mobiles = userInfoService.mobiles();
        for (String mobile : mobiles) {
            restAPI.sendTemplateSMS(mobile, "42498", new String[]{""});
        }
    }
}
